//
//  ScreenView.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 10/21/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "ScreenView.h"
#import "ScreenCell.h"
#import "ChooseCameraManViewController.h"
@implementation ScreenView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self makeViews];
    }
    return self;
}

-(void)makeViews
{
    //最底层
    self.backView = [UIView initViewRect:self.bounds andBackGroudColor:RGBA(0, 0, 0,0.3)];
    [self addSubview:_backView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenTheView:)];
    [_backView addGestureRecognizer:tap];
    
    //带三角底层
    self.bgView = [UIImageView initImageViewRect:CGRectMake(0, 0, UI_SCREEN_WIDTH, 200) andImage:nil andUserInteractionEnabled:YES];
    _bgView.backgroundColor = UIColorFromRGB(0xf1f1f1);
    [self addSubview:_bgView];
    
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenTheView:)];
    [_bgView addGestureRecognizer:tap2];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing =0;
    flowLayout.minimumLineSpacing =12;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    mCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(22.5,22.5,75*3+50, 100) collectionViewLayout:flowLayout];
    [mCollectionView registerClass:[ScreenCell class] forCellWithReuseIdentifier:@"cell"];
    mCollectionView.backgroundColor = [UIColor clearColor];
    mCollectionView.alwaysBounceVertical = YES;
    mCollectionView.bounces = NO;
    mCollectionView.delegate = self;
    mCollectionView.dataSource = self;
    mCollectionView.hidden = NO;
    [self addSubview:mCollectionView];
}

#pragma mark UICollectionView Delegate
//设置分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//每个分区上的元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_dataArray count];
}

//设置元素内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    ScreenCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.myBtn.enabled = YES;
    
    if ([_type isEqualToString:@"taocan"]) {
        cell.swdataDict = [_dataArray objectAtIndex:indexPath.row];
    }else if ([_type isEqualToString:@"dizhi"]) {
        [cell setSwdataDict:_dataArray[indexPath.row] withKey:@"CityName"];
    }else if ([_type isEqualToString:@"jiage"] || [_type isEqualToString:@"other"]) {
        [cell setSwdataDict:_dataArray[indexPath.row] withKey:@"name"];
    }else{
        cell.swdataDict = [_dataArray objectAtIndex:indexPath.row];
    }
    
    [cell.myBtn addTarget:self action:@selector(chooseTheBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.myBtn.tag = 1000+indexPath.row;
    if ([cell.myBtn.titleLabel.text isEqualToString:_selectName]){
        cell.myBtn.enabled = NO;
        _selectIndex = indexPath.row;
    }else{
        cell.myBtn.enabled = YES;
    }
    
    return cell;
}

//设置元素的的大小框
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UIEdgeInsets top ={0,0,0,0};
    return top;
}

//设置顶部的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={0,0};
    return size;
}

//设置元素大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(75,30);
}


-(void)chooseTheBtn:(UIButton *)button
{
    button.enabled = NO;
    self.selectName =  button.titleLabel.text;
    
    if ([_type isEqualToString:@"taocan"]) {
        [_delegate chooseType:_type data:_dataArray[button.tag-1000]];
    }else if ([_type isEqualToString:@"dizhi"]) {
        [_delegate chooseType:_type data:_dataArray[button.tag-1000]];
    }else if ([_type isEqualToString:@"other"]) {
        [_delegate chooseType:_type data:_dataArray[button.tag-1000]];
    }else if ([_type isEqualToString:@"jiage"]) {
        [_delegate chooseType:_type data:_dataArray[button.tag-1000]];
    }else{
        [_delegate whileScreenViewIsHidden:[_dataArray objectAtIndex:button.tag-1000] isReflash:YES];
    }
    
    [mCollectionView reloadData];
    [self hiddenTheScreenView];
}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    if(_dataArray){
        _dataArray = nil;
    }
    _dataArray = [[NSMutableArray alloc] initWithArray:dataArray];
    
    mCollectionView.frame = CGRectMake(22.5,22.5,75*3+50,(([_dataArray count]-1)/3+1)*(30+12));
    _bgView.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH,mCollectionView.frame.size.height+22.5+22.5-12);
    
    [mCollectionView reloadData];
}

-(void)setSelectName:(NSString *)selectName
{
    if (_selectName){
        _selectName = nil;
    }
    _selectName = [[NSString alloc] initWithString:selectName];
    
    [mCollectionView reloadData];
}

-(void)hiddenTheView:(UITapGestureRecognizer *)tap
{
    [self hiddenTheScreenView];
    [_delegate whileScreenViewIsHidden:[_dataArray objectAtIndex:_selectIndex]  isReflash:NO];
    if (IS_NOT_EMPTY(_type)) {
        ChooseCameraManViewController * vc = (ChooseCameraManViewController *)_delegate;
        [vc makeSanJiaoWithImage:[UIImage imageNamed:@"downsanjiao_dark"]];
    }
}

//隐藏
-(void)hiddenTheScreenView
{
    if (self.isHidden == NO){
        self.hidden = YES;
    }
}

//显示
-(void)showTheScreenView
{
    if (self.isHidden == YES){
        self.hidden = NO;
    }
}





#pragma mark - mby's
- (void)hideSelf{
    [_delegate chooseType:@"hide" data:nil];
}

- (void)showWithType:(NSString *)type array:(NSArray *)array title:(NSString *)title{
    return;
    self.hidden = NO;
    titleStr = title;
    if ([type isEqualToString:@"taocan"]) {
        _dataArray = [NSMutableArray arrayWithArray:array];
    }else if ([type isEqualToString:@"dizhi"]) {
        _dataArray = [NSMutableArray arrayWithArray:array];
    }else if ([type isEqualToString:@"other"]) {
        _dataArray = [NSMutableArray arrayWithArray:@[@{@"name":@"综合排序",@"type":@""},
                                                      @{@"name":@"离我最近",@"type":@"Range"},
                                                      @{@"name":@"接单量",@"type":@"OrderCount"},
                                                      @{@"name":@"好评率",@"type":@"ReviewAvg"},
                                                      @{@"name":@"价格最高",@"type":@"PriceTop"},
                                                      @{@"name":@"价格最低",@"type":@"PriceLow"}]];
    }
    [mCollectionView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
